package com.example.com.test.widgets;

import android.app.Activity;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.text.TextUtils;

import com.sensorsdata.analytics.android.sdk.SensorsDataAPI;
import com.sensorsdata.analytics.android.sdk.util.SensorsDataUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Locale;
import java.util.Map;

/**
 * 埋点工具类
 */
public class TrackUtils {
    private static String SCREEN_NAME = "$screen_name";

    /**
     * 自定义$screen_name的埋点<br/>
     * 注意：key不能为以下值：
     * distinct_id,original_id,time,properties,id,first_id,second_id,users,events,event,user_id,date,datetime
     * @param map 键值对，需要在页面中额外记录的数据
     */
    @SuppressWarnings("all")
    public static void trackViewScreen(@NonNull Map<String, String> map, @NonNull String screenName) {
        if (map == null) return;
        JSONObject jsonObj = new JSONObject();
        try {
            for (String key : map.keySet()) {
                jsonObj.put(key, map.get(key));
            }
            jsonObj.put(SCREEN_NAME, screenName);
            if (SensorsDataAPI.sharedInstance() != null) {
                SensorsDataAPI.sharedInstance().trackViewScreen(null, jsonObj);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * <p>Description:没有额外参数时调用的方法</p>
     *
     */
    @SuppressWarnings("all")
    public static void trackViewScreenNoParams(@NonNull String screenName) {
        JSONObject jsonObj = new JSONObject();
        try {
            jsonObj.put(SCREEN_NAME, screenName);
            if (SensorsDataAPI.sharedInstance() != null) {
                SensorsDataAPI.sharedInstance().trackViewScreen(null, jsonObj);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 记录页面打开的埋点
     *
     * @param map 键值对，需要在页面中额外记录的数据
     */
    @SuppressWarnings("all")
    public static void trackViewScreen(@NonNull Map<String, String> map, Activity activity) {
        if (map == null) return;
        JSONObject jsonObj = new JSONObject();
        try {
            for (String key : map.keySet()) {
                jsonObj.put(key, map.get(key));
            }
            jsonObj.put(SCREEN_NAME, activity.getClass().getCanonicalName());
            SensorsDataAPI.sharedInstance().trackViewScreen(null, jsonObj);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("all")
    public static void trackViewScreen(@NonNull Map<String, String> map, Fragment fragment) {
        if (map == null) return;
        try {
            if (fragment == null) {
                return;
            }
            JSONObject properties = new JSONObject();
            String fragmentName = fragment.getClass().getCanonicalName();
            String screenName = fragmentName;

            if (Build.VERSION.SDK_INT >= 11) {
                Activity activity = fragment.getActivity();
                if (activity != null) {
                    String activityTitle = SensorsDataUtils.getActivityTitle(activity);
                    if (!TextUtils.isEmpty(activityTitle)) {
                        properties.put("$title", activityTitle);
                    }
                    screenName = String.format(Locale.CHINA, "%s|%s", activity.getClass().getCanonicalName(), fragmentName);
                }
            }
            properties.put(SCREEN_NAME, screenName);
            for (String key : map.keySet()) {
                properties.put(key, map.get(key));
            }
            SensorsDataAPI.sharedInstance().trackViewScreen(null, properties);
        } catch (Exception e) {

        }
    }

    /**
     * 直接上传数据统计
     *
     * @param json 完整的统计事件，json中包含$screen_name，不做额外处理
     */
    @SuppressWarnings("all")
    public static void trackDirect(@NonNull String json) {
        if (TextUtils.isEmpty(json)) {
            return;
        }
        try {
            JSONObject jsonObject = new JSONObject(json);
            SensorsDataAPI.sharedInstance().trackViewScreen(null, jsonObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
